package com.filldream.fastboot.common.util;

import cn.hutool.captcha.generator.MathGenerator;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * @author RickSun
 * @Description 随机字符串
 **/
public class RandomUtil {
    private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String ARABNUMBER = "0123456789";
    private static final String ALPHANUM = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String ALPHANUMSYMBOL = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ！@￥%……&*()-——+=#$%^&*;',.";

    public RandomUtil() {
    }

    /**
     * @author RickSun
     * @Description 随机字母
     * @param len 随机长度
     **/
    public static String randomAlphbetString(int len) {
        if (len < 1) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for(int i = 0; i < len; ++i) {
            sb.append(ALPHABET.charAt(random.nextInt(ALPHABET.length())));
        }
        return sb.toString();
    }

    /**
     * @author RickSun
     * @Description 随机阿拉伯数字
     * @param len 随机长度
     **/
    public static String randomNumberString(int len) {
        if (len < 1) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for(int i = 0; i < len; ++i) {
            sb.append(ARABNUMBER.charAt(random.nextInt(ARABNUMBER.length())));
        }
        return sb.toString();
    }

    /**
     * @author RickSun
     * @Description 随机指定范围内的阿拉伯数字
     * @param start 开始区间
     * @param end 结束区间
     **/
    public static Integer randomNumByInterval(int start, int end) {
        if (start > end) {
            Integer t = start;
            start = end;
            end = t;
        }
        Random random = new Random();
        return random.nextInt(end) % (end - start + 1) + start;
    }

    /**
     * @author RickSun
     * @Description 随机指定范围内的阿拉伯数字
     * @param start 开始区间
     * @param end 结束区间
     * @param containLeft 是否包含开始数字
     * @param containRight 是否包含结束数字
     **/
    public static Integer randomNumByInterval(int start, int end, Boolean containLeft, Boolean containRight) {
        if (start > end) {
            Integer t = start;
            start = end;
            end = t;
        }
        Random random = new Random();
        Integer result = random.nextInt(end) % (end - start + 1) + start;
        if (containLeft && !containRight) {
            while(result == end) {
                result = random.nextInt(end) % (end - start + 1) + start;
            }
        } else if (!containLeft && containRight) {
            while(result == start) {
                result = random.nextInt(end) % (end - start + 1) + start;
            }
        } else if (!containRight && !containRight) {
            while(result == start || result == end) {
                result = random.nextInt(end) % (end - start + 1) + start;
            }
        }
        return result;
    }

    /**
     * @author RickSun
     * @Description 随机阿拉伯数字和字母混合字符串
     * @param len 随机长度
     **/
    public static String randAlphaNumString(int len) {
        if (len < 1) {
            return "";
        } else {
            StringBuffer sb = new StringBuffer();
            Random random = new Random();
            for(int i = 0; i < len; ++i) {
                sb.append(ALPHANUM.charAt(random.nextInt(ALPHANUM.length())));
            }
            return sb.toString();
        }
    }

    /**
     * @author RickSun
     * @Description 自定义随机字符串
     * @param fromString 随机字符串数据
     * @param len 随机长度
     **/
    public static String randDesignatedString(String fromString, int len) {
        if (StringUtils.isBlank(fromString)) {
            return "";
        } else if (fromString.length() < 2) {
            return fromString;
        } else if (len < 1) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Random random = new Random();

        for(int i = 0; i < len; ++i) {
            sb.append(fromString.charAt(random.nextInt(fromString.length())));
        }
        return sb.toString();
    }

    /**
     * @author RickSun
     * @Description UUID生成
     **/
    public static String randomUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * @author RickSun
     * @Description 生成三则预算表达
     **/
    public static Map<String,String> randMath(int begin, int end){
        Map<String,String> map = new HashMap<>();
        String type = randDesignatedString("+-*", 1);
        int leftNum = randomNumByInterval(begin, end, true, true);
        int rightNum = randomNumByInterval(begin, end, true, true);
        if( type.equals("+") ){
            map.put("result",(leftNum + rightNum) + "");
        }else if( type.equals("-") ){
            map.put("result",(leftNum - rightNum) + "");
        }else if( type.equals("*") ){
            map.put("result",(leftNum * rightNum) + "");
        }
        map.put("ex",leftNum + type + rightNum );
        return map;
    }


}
